<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>客户编辑</title>
<link href="/resources/lib/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
<link href="/resources/lib/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" />
<script src="/resources/lib/jquery/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="/resources/lib/ligerUI/js/ligerui.all.js" type="text/javascript"></script>
<!-- <script src="/resources/lib/ligerUI/js/plugins/ligerForm.js" type="text/javascript"></script> -->
<script src="/resources/lib/jquery-validation/jquery.validate.min.js" type="text/javascript"></script> 
<script src="/resources/lib/jquery-validation/jquery.metadata.js" type="text/javascript"></script>
<script src="/resources/lib/jquery-validation/messages_cn.js" type="text/javascript"></script>
<script src="/resources/lib/json2.js" type="text/javascript"></script>
<script src="/resources/js/jsF7.js" type="text/javascript"></script>
<script src="/resources/js/jquery.cityselect.js" type="text/javascript"></script>
<script src="/resources/js/selectData.js" type="text/javascript"></script>
<script src="/resources/js/themis.js" type="text/javascript"></script>
<script src="/resources/js/validation.js" type="text/javascript"></script>
<style type="text/css">
    .l-table-edit {}
    .l-table-edit-td-label{width: 100px;padding-top: 5px;}
    .l-table-edit-td-input{width: 200px;padding-top: 5px;}
    .div-buttom{position: absolute;top:380px;}
    
</style>
<script type="text/javascript">
var form1 = null;
var grid = null;
var toolbar = null;
var info = '${info}';
var existCustomer = null;
if(info){
	existCustomer = JSON.parse(info); 
}
var status = '${status}';
var number = '${number}';
var customerProperUuid ='${customerProperUuid}';

 
$(function (){
    //工具条
    toolbar = $("#toptoolbar").ligerToolBar({ items: [
        { text: '保存', id:'save',click: saveForm,icon: 'save' }
    ]
    });
  //创建表单(头部)
    form1 = $("#form1").ligerForm({
    inputWidth: 170, labelWidth: 90, space: 40,
    validate:true,
    fields: [
        { name: "uuid", type: "hidden" },
        { name: "createUser.uuid", type: "hidden" },
        { name: "createDate", type: "hidden" },
        { display: "编号", name: "number", type: "text",newline: false, validate: {remote:{type:"POSt",url:"/customer/checkNumberUsed",
      	  data:{uuid:function(){
        	  var uuidEd = form1.getEditor("uuid"); //form中应用
              var uuid = uuidEd.val();
        	  return uuid;
        	  }}},required: true,minlength: 1},validateMessage:{remote:"此编码已经存在"}},
        { display: "名称", name: "name", type: "text",newline: false,  validate: { required: true} },
        { display: "客户分类", name: "customerProper.uuid", newline: false, type: "popup",textField:"customerProper.name",
     	   editor: {
                grid: getCustomerClassificationGrid(true),
                valueField:'uuid',
                textField: 'name',
                parms :{leaf:'1',date:new Date().getTime()},
                width: 300,
                height:100,
                realTimeLoad:true
       	 }   
        },
        { display: "客户性质", name: "customerNature", newline: false,type: "select",editor: {data:customerNature,cancelable:false,initValue:"personage",valueField: 'value',
     	   onSelected  :function(value,text){
					if(form1){
						if(value == "company"){
							/*  var expiraData = form1.getEditor("inventory.expiraData"); //form中应用
							 expiraData.setValue(null);
							 form1.setFieldValidate("inventory.expiraData",{required:false},{});
	                    	  var expiraDataUtil = form1.getEditor("inventory.expiraDataUtil"); //form中应用
	                    	  expiraDataUtil.setText(null);
	                    	  expiraDataUtil.setValue(null);
	                    	  form1.setFieldValidate("inventory.expiraDataUtil",{required:false},{});
	                    	  form1.setEnabled(["inventory.expiraDataUtil","inventory.expiraData"],false); */
	                    	 /*  var personalName = form1.getEditor("personalName"); //form中应用
	                    	  personalName.setValue(null);
	                    	  personalName.value=null;
							  var personalPhone = form1.getEditor("personalPhone"); //form中应用
							  personalPhone.setValue(null);
							  personalPhone.value=null;
							  var personalEmail = form1.getEditor("personalEmail"); //form中应用
							  personalEmail.setValue(null);
							  personalEmail.value=null;
							  var personalIdcard = form1.getEditor("personalIdcard"); //form中应用
							  personalIdcard.setValue(null);
							  personalIdcard.value=null;
							  var personalMobilePhone = form1.getEditor("personalMobilePhone"); //form中应用
							  personalMobilePhone.setValue(null);
							  personalMobilePhone.value=null;
							  var manageNum = form1.getEditor("qq"); //form中应用
							  qq.setValue(null);
							  qq.value=null;
							  var weChat = form1.getEditor("weChat"); //form中应用
							  weChat.setValue(null);
							  weChat.value=null;
							  var whetherVip = form1.getEditor("whetherVip"); //form中应用
							  whetherVip.setValue(null);
							  whetherVip.value=null;
							  var vipNumber = form1.getEditor("vipNumber"); //form中应用
							  vipNumber.setValue(null);
							  vipNumber.value=null; */
							  showFields(["industrialNum", "legalPerson", "tradingNum", "manageNum","finance.accountingPerson",
							              "finance.personPosition","finance.phone","finance.mobile","finance.mail","finance.openBank.uuid",
							              "finance.bankAccount","finance.currency.uuid","finance.accountName","sale.contactPerson","sale.personPosition",
							              "sale.phone","sale.mobile","sale.address","makeType","taxRate","taxType"],form1);
							  hideFields(["personalName", "personalPhone", "personalEmail", "personalIdcard","personalMobilePhone","qq","weChat","whetherVip","vipNumber"],form1);
							 /* form1.setVisible(["personalName", "personalPhone", "personalEmail", "personalIdcard","personalMobilePhone","qq","weChat","whetherVip","vipNumber"], false);
							 form1.setVisible(["industrialNum", "legalPerson", "tradingNum", "manageNum"], true); */
						}else if(value == "personage"){
							/* form1.setFieldValidate("inventory.expiraData",{required: true},{});
							form1.setFieldValidate("inventory.expiraDataUtil",{required: true},{});
							 form1.setEnabled(["inventory.expiraDataUtil","inventory.expiraData"],true); */
							/*  var industrialNum = form1.getEditor("industrialNum"); //form中应用
							 industrialNum.setValue(null);
							 industrialNum.value=null;
							  var legalPerson = form1.getEditor("legalPerson"); //form中应用
							  legalPerson.setValue(null);
							  legalPerson.value=null;
							  var tradingNum = form1.getEditor("tradingNum"); //form中应用
							  tradingNum.setValue(null);
							  tradingNum.value=null;
							  var manageNum = form1.getEditor("manageNum"); //form中应用
							  manageNum.setValue(null);
							  manageNum.value=null; */
							  
							  hideFields(["industrialNum", "legalPerson", "tradingNum", "manageNum","finance.accountingPerson",
							              "finance.personPosition","finance.phone","finance.mobile","finance.mail","finance.openBank.uuid",
							              "finance.bankAccount","finance.currency.uuid","finance.accountName","sale.contactPerson","sale.personPosition",
							              "sale.phone","sale.mobile","sale.address","makeType","taxRate","taxType"],form1);
							  showFields(["personalName", "personalPhone", "personalEmail", "personalIdcard","personalMobilePhone","qq","weChat","whetherVip","vipNumber"],form1);
							
							  /* form1.setVisible(["personalName", "personalPhone", "personalEmail", "personalIdcard","personalMobilePhone","qq","weChat","whetherVip","vipNumber"], true);
							 form1.setVisible(["industrialNum", "legalPerson", "tradingNum", "manageNum"], false); */
						}
					}
				}}},
				
				/* { display: "上级客户", name: "superCustomer.uuid", newline: false,  group: "基础信息", type: "popup",textField:"superCustomer.name",
             	   editor: {
	                       grid: getCustomerGrid(true),
	                       valueField:'uuid',
	                       textField: 'name',
	                       parms :{date:new Date().getTime()},
	                       width: 300,
	                       height:100,
	                       realTimeLoad:true
	              	 }   
                }, */
                { display: "所属客户经理", name: "subCustomerManager.uuid", newline: false,  group: "基础信息", type: "popup",textField:"subCustomerManager.trueName",
              	   editor: {
 	                       grid: getSysUserGrid(true),
 	                       valueField:'uuid',
 	                       textField: 'trueName',
 	                       parms :{date:new Date().getTime()},
 	                       width: 300,
 	                       height:100,
 	                       realTimeLoad:true
 	              	 }   
                 },
                { display: "税务登记号", name: "industrialNum", newline: false, type: "text"},
                { display: "法人代表", name: "legalPerson", newline: false, type: "text"},
                { display: "营业执照", name: "tradingNum", newline: false, type: "text"},
                { display: "经营许可证", name: "manageNum", newline: false, type: "text"},
                
                { display: "姓名", name: "personalName", newline: false, type: "text"},
                { display: "电话", name: "personalPhone", newline: false, type: "text"},
                { display: "邮箱", name: "personalEmail", newline: false, type: "text"},
                { display: "身份证", name: "personalIdcard", newline: false, type: "text"},
                { display: "手机", name: "personalMobilePhone", newline: false, type: "text"},
                { display: "QQ", name: "qq", newline: false, type: "text"},
                { display: "微信", name: "weChat", newline: false, type: "text"},
                { display: "是否为会员", name: "whetherVip", newline: false, type: "select",editor: {data:true_false_data,cancelable:false,initValue:"false",valueField: 'value',textField: 'text'}},
                { display: "会员编号", name: "vipNumber", newline: false, type: "text"},
                
                
                { display: "行业", name: "industry", newline: false, type: "text"},
                { display: "邮政编码", name: "zipCode", newline: false, type: "text"},
                { display: "开票类型", name: "makeType", newline: false,  type: "select",editor: {data:billing_type,
						valueField: 'value',textField: 'text'
					}},
                { display: "税率", name: "taxRate", newline: false, type: "text"},
                { display: "税种", name: "taxType", newline: false, type: "text"},
                { display: "国家", name: "country", newline: false, type: "select",editor: {}},
                { display: "省份", name: "province", newline: false, type: "select",editor: {}},
                { display: "城市(区)", name: "city", newline: false, type: "select",editor: {}},
                { display: "地区(县)", name: "district", newline: false, type: "select",editor: {}},
                /* { display: "地址", name: "address", newline: true, type: "textarea",width:470} */
                { display: "地址", name: "address", newline: true, type: "text",width:470} ,
                
                { display: "会计联系人", name: "finance.accountingPerson",  group: "财务资料", newline: false, type: "text"}, 
				{ display: "联系人职务", name: "finance.personPosition", newline: false, type: "text"}, 
				{ display: "办公电话", name: "finance.phone", newline: false, type: "text"}, 
				{ display: "手机", name: "finance.mobile", newline: false, type: "text"}, 
				{ display: "邮件", name: "finance.mail", newline: false, type: "text"}, 
				/* { display: "开户银行", name: "finance.openBank", newline: false, type: "text"},  */
				{ display: "开户银行", name: "finance.openBank.uuid", newline: false, type: "popup",textField:"finance.openBank.name",
                	   editor: {
	                       grid: getBankGrid(true),
	                       valueField:'uuid',
	                       textField: 'name',
	                       parms :{date:new Date().getTime()},
	                       width: 300,
	                       height:100,
	                       realTimeLoad:true
	              	 }   
                 },
				{ display: "银行账号", name: "finance.bankAccount", newline: false, type: "text"},
				/* { display: "币别", name: "finance.currency", newline: false, type: "text"}, */
				{ display: "币别", name: "finance.currency.uuid", newline: false, type: "popup",textField:"finance.currency.name",
                	   editor: {
	                       grid: getCurrencyGrid(true),
	                       valueField:'uuid',
	                       textField: 'name',
	                       parms :{date:new Date().getTime()},
	                       width: 300,
	                       height:100,
	                       realTimeLoad:true
	              	 }   
                 },
				{ display: "账户名称", name: "finance.accountName", newline: false, type: "text"},
				
				{ display: "联系人", name: "sale.contactPerson", group:"销售资料", newline: false, type: "text"},
				{ display: "联系人职务", name: "sale.personPosition", newline: false, type: "text"},
				{ display: "办公电话", name: "sale.phone", newline: false, type: "text"},
				{ display: "手机", name: "sale.mobile", newline: false, type: "text"},
				{ display: "地址", name: "sale.address", newline: true, type: "text",width:470}
        ]/* ,
        tab: {
            items : [
                {
                    title: '基本信息', fields: [
                            { display: "业务分析码", name: "businessNum", newline: false, type: "text"},
                           { display: "内部客户", name: "inerCustomer", newline: false, type: "text"},
                           { display: "内部公司", name: "inerCompany", newline: false, type: "text"}, 
                            { display: "客户性质", name: "customerProper", newline: false, type: "text"}, 
                          
                            { display: "上级客户", name: "superCustomer", newline: false, type: "text"}, 
                           
                    ]
                },
                {
                    title: '财务资料', fields: [
							 { display: "所属公司编码", name: "finance.suCompanyNum", newline: false, type: "text"}, 
							{ display: "所属公司名称", name: "finance.suCompanyName", newline: false, type: "text"},  
							
                    ]
                },
                {
                    title: '销售资料', fields: [
							 { display: "所属公司编码", name: "sale.suCompanyNum", newline: false, type: "text"}, 
							{ display: "所属公司名称", name: "sale.suCompanyName", newline: false, type: "text"}, 
							{ display: "应收客户", name: "sale.ysCustomer", newline: false, type: "text"},
							{ display: "收款客户", name: "sale.skCustomer", newline: false, type: "text"},
							{ display: "送货客户", name: "sale.deliverCustomer", newline: false, type: "text"}, 
							
                    ]
                }
            ]
        } */
    });
  
    //创建分录
    grid = $("#grid").ligerGrid({
        columns: [
			{ display: "联系人", name: "contactPerson", minWidth:100,type: "text",editor: { type: 'text' }},
			{ display: "联系人职务", name: "personPosition", minWidth:100, type: "text",editor: { type: 'text' }},
			{ display: "办公电话", name: "phone", minWidth:100, type: "text",editor: { type: 'text' }},
			{ display: "手机", name: "mobile", minWidth:100, type: "text",editor: { type: 'text' }},
			{ display: "地址", name: "address", minWidth:100, type: "text",editor: { type: 'text' }}
        ], usePager: false,width: '99%',height: '98%',enabledEdit: true,alternatingRow:true,
        checkbox: true,headerRowHeight:22, rowHeight:22, rownumbers:true,
        onAfterEdit: f_onAfterEdit,

        toolbar: { items: [
                           { text: '添加行', click: addNewRow,icon: 'add' },
                           { line: true },
                           { text: '删除行', click: delRow,icon: 'delete' }
                           ]
                 }
        
    });
  if(existCustomer){
	  form1.setData(existCustomer);
	  grid.set({ data: {Rows:existCustomer.entrys}}); 
	  try{
		  if(existCustomer.customerNature == "" || typeof(existCustomer.customerNature) =="undefined"){
			  
			  hideFields(["industrialNum", "legalPerson", "tradingNum", "manageNum","finance.accountingPerson",
			              "finance.personPosition","finance.phone","finance.mobile","finance.mail","finance.openBank.uuid",
			              "finance.bankAccount","finance.currency.uuid","finance.accountName","sale.contactPerson","sale.personPosition",
			              "sale.phone","sale.mobile","sale.address","makeType","taxRate","taxType"],form1);
			  showFields(["personalName", "personalPhone", "personalEmail", "personalIdcard","personalMobilePhone","qq","weChat","whetherVip","vipNumber"],form1);
			
			  
			 /*  form1.setVisible(["personalName", "personalPhone", "personalEmail", "personalIdcard"], false);
			  form1.setVisible(["industrialNum", "legalPerson", "tradingNum", "manageNum"], true); */
			 /*  form1.setEnabled(["inventory.expiraDataUtil","inventory.expiraData"],false);
			  existCustomer.inventory.expiraData = null;
			  existCustomer.inventory.expiraDataUtil = null; */
		  }else if(existCustomer.customerNature == "company"){
			  showFields(["industrialNum", "legalPerson", "tradingNum", "manageNum","finance.accountingPerson",
			              "finance.personPosition","finance.phone","finance.mobile","finance.mail","finance.openBank.uuid",
			              "finance.bankAccount","finance.currency.uuid","finance.accountName","sale.contactPerson","sale.personPosition",
			              "sale.phone","sale.mobile","sale.address","makeType","taxRate","taxType"],form1);
			  hideFields(["personalName", "personalPhone", "personalEmail", "personalIdcard","personalMobilePhone","qq","weChat","whetherVip","vipNumber"],form1);
		  }
	  }catch(e){
		  hideFields(["industrialNum", "legalPerson", "tradingNum", "manageNum","finance.accountingPerson",
		              "finance.personPosition","finance.phone","finance.mobile","finance.mail","finance.openBank.uuid",
		              "finance.bankAccount","finance.currency.uuid","finance.accountName","sale.contactPerson","sale.personPosition",
		              "sale.phone","sale.mobile","sale.address","makeType","taxRate","taxType"],form1);
		  showFields(["personalName", "personalPhone", "personalEmail", "personalIdcard","personalMobilePhone","qq","weChat","whetherVip","vipNumber"],form1);
		
		  console.log(e);
	  }
  }else{
	  hideFields(["industrialNum", "legalPerson", "tradingNum", "manageNum","finance.accountingPerson",
	              "finance.personPosition","finance.phone","finance.mobile","finance.mail","finance.openBank.uuid",
	              "finance.bankAccount","finance.currency.uuid","finance.accountName","sale.contactPerson","sale.personPosition",
	              "sale.phone","sale.mobile","sale.address","makeType","taxRate","taxType"],form1);
	  showFields(["personalName", "personalPhone", "personalEmail", "personalIdcard","personalMobilePhone","qq","weChat","whetherVip","vipNumber"],form1);
	
	  /* form1.setVisible(["personalName", "personalPhone", "personalEmail", "personalIdcard"], false);
	  form1.setVisible(["industrialNum", "legalPerson", "tradingNum", "manageNum"], true); */
	  
	  if(number){
		  var numberE = form1.getEditor("number"); //form中应用
		  numberE.setValue(number);
		 // form1.setData({number:number});
	  }
	  if(customerProperUuid){
		  var e = form1.getEditor("customerProper.uuid"); //form中应用
		  e.setValue(customerProperUuid,null);
	  }
  }
  
  if(status && status =='1'){
	  toolbar.setDisabled('save');
	  form1.setAllEnabled(false);
	  grid.setEnabledEdit(false);
  }
  
  citySelect({
	  countryEditers:[form1.getEditor("country")],
	  provEditers:[form1.getEditor("province")],
	  cityEditers:[form1.getEditor("city")],
	  distEditers:[form1.getEditor("district")],
  });
});

function showFields(arr,form){
	if(arr){
		 form1.setVisible(arr, true);
	}
}
function hideFields(arr,from){
	if(arr){
		for(var i=0;i<arr.length;i++){
			 var e = form1.getEditor(arr[i]);
			 if(e){
				 e.setValue(null);
				 e.value=null;
				if(e.setText){
					 e.setText(null);
					 e.text=null;
				}
			 }
		}
		form1.setVisible(arr, false);
	}
}

//保存
function saveForm(){
   // var forms = $('#form1').serialize(); 
    var data = form1.getData();
    var gridData = grid.getData();
   /*  delete data.customerProperName;
    delete data.superCustomerName;
    delete data.finance.openBankName;
    delete data.finance.currencyName; */
   // data = JSON.stringify(data);
    console.log(data);
    if(form1.valid()){
    	data.entrys = gridData;
	 	$.ajax({
	        loading: '正在保存数据中...',
	        type:'post',
	        url: "/customer/saveOrUpdateCustomer",
			dataType:'json',
	        data: {customer:JSON.stringify(data)},
	        success: function (data) {
					$.ligerDialog.success(data.message,'信息',function(){
						$("#uuid").val(data.data.uuid);
				    });
	        },
	        error: function (message) {
	       	 	$.ligerDialog.error("保存失败");
	        }
	    });
    }
}
</script>
</head>
<body>
	<div id="toptoolbar"></div>
	<form id="form1"></form>
	<div id="grid" style="margin: 8px;"></div>
</body>
<script type="text/javascript">
function addNewRow(){
	grid.add();
}
//删除分录
function delRow(){
	grid.deleteSelectedRow();
}
function f_onAfterEdit(e){
	
}
</script>
</html>